//
//  SFHomeTableViewController.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/7.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit
import SVProgressHUD

class SFHomeTableViewController: SFBasicTableViewController{

    //cell的标示
    private let SFHOMECELLID = "SFHomeCellID"


    //statusViewModel
    lazy var statusViewModel:SFStatusListViewModel = SFStatusListViewModel()

//    var statusArray:[SFStatus]?

    override func viewDidLoad() {
        super.viewDidLoad()


        //模型数组

        if isLogin == false {

            visitorView.setInfo(nil, message: nil)

            return

        }

        //更新UI
        setupUI()
        //加载数据
        loadData()
    }



    //MARK: - 私有方法

    private func setupUI(){

        self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", title: nil, target: nil, SEL: nil)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", title: nil, target: self, SEL: #selector(SFHomeTableViewController.push))

        //隐藏分割线
        self.tableView.separatorStyle = .None

        setupTableView()


        
    }

    private func setupTableView(){

        tableView.registerClass(SFStatusCell.self, forCellReuseIdentifier: SFHOMECELLID)

//        tableView.rowHeight = 250
        //设置自动调整高度(需要同时设置预估高度)
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估高度
        tableView.estimatedRowHeight = 300
        //设置上拉加载的控件
        tableView.tableFooterView = pullUpView

        //设置下拉刷新的控件
//        self.refreshControl = refreshView
        tableView.addSubview(refreshView)


    }

    func push(){

        navigationController?.pushViewController(SFTempVc(), animated: true)
    }


    @objc private func loadData(){

        SVProgressHUD.showWithStatus("玩命加载中")

        self.statusViewModel.loadData(pullUpView.isAnimating(), finished: { (error) in

//            self.refreshView.refreshState = .Normal

            if error != nil {

                print("请求失败\(error)")

                SVProgressHUD.dismiss()

                SVProgressHUD.showErrorWithStatus("万分抱歉,加载失败")

                self.endAnimation()
                
                return
            }

            //请求成功,刷新界面
            self.endAnimation()

            self.tableView.reloadData()

            SVProgressHUD.dismiss()

            SVProgressHUD.showSuccessWithStatus("恭喜,加载成功")
        })
    }

    func endAnimation() {

        self.pullUpView.stopAnimating()
//        self.refreshControl?.endRefreshing()
        self.refreshView.endRefresh()
    }


    //MARK : - 懒加载上拉加载的控件
    lazy var pullUpView : UIActivityIndicatorView = {

        let pullUpView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)

        pullUpView.color = UIColor.purpleColor()

        return pullUpView
    }()
    // 下拉刷新的控件
//    lazy var refreshView: UIRefreshControl = {
//
//        let refresh = UIRefreshControl()
//
//        refresh.addTarget(self, action: #selector(SFHomeTableViewController.loadData), forControlEvents: .ValueChanged)
//
//        return refresh
//    }()
    //自定义的下拉刷新的控件
    lazy var refreshView: SFRefreshControl = {

        let r = SFRefreshControl()

        r.addTarget(self, action: #selector(SFHomeTableViewController.loadData), forControlEvents: .ValueChanged)

        return r
    }()

}


//MARK: - tableView代理

extension SFHomeTableViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.statusViewModel.statusArray?.count ?? 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(SFHOMECELLID, forIndexPath: indexPath) as! SFStatusCell

//        let model = statusViewModel.statusArray![indexPath.row]

//        cell.textLabel?.text = model.user?.screen_name

        let status :SFStatusViewModel? = self.statusViewModel.statusArray![indexPath.row]

        if status != nil{

        cell.statusViewModel = status
        }

//        cell.statusViewModel = statusViewModel.statusArray![indexPath.row]

        return cell
    }

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {


        //上拉加载
        if indexPath.row == (statusViewModel.statusArray?.count)! - 1 {


            pullUpView.startAnimating()

            loadData()
        }
    }
}




